# Load Python libraries
import numpy as np
from PIL import Image
# Load Plot libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Loading example image
file_path = "../data/img/example-1.png"
img = Image.open(file_path)
# Show dimensions (resolution)
img.size
# Show image
img
# Read file in low level (bit list)
with open(file_path, 'rb') as f:
low_bit_list = [byte & 1 for byte in bytearray(f.read())]
# Show size (KB)
round(len(low_bit_list) / 1024, 2)
# Show size (MB)
round(len(low_bit_list) / 1024 / 1020, 2)
# Create a matrix
row_len = 2232
col_len = 1252
matrix = np.zeros((row_len, col_len))
matrix.shape
# Calculate additional bits
gap = np.prod(matrix.shape) - len(low_bit_list)
gap
# Save bits into matrix
data = np.array(low_bit_list)
for i in range(0, len(data)):
ix_row = int(i / col_len)
ix_col = i % col_len
matrix[ix_row][ix_col] = data[i]
# Plot image in binary
fig, ax = plt.subplots(figsize = (14, 14))
sns.heatmap(matrix, ax = ax)
ax.set_title("Image in Binary", fontsize = 16)
ax.set_xlabel('columns', fontsize = 12)
ax.set_ylabel('rows', fontsize = 12)
plt.show()